package com.dwjf.core.ui;

import android.content.Context;

import com.wang.avi.AVLoadingIndicatorView;
import com.wang.avi.Indicator;

import java.util.WeakHashMap;
import java.util.concurrent.ExecutionException;

public final class LoaderCreator {
    private static final WeakHashMap<String, Indicator> LOADING_MAP = new WeakHashMap<>();


    static AVLoadingIndicatorView create(String type, Context context){
        AVLoadingIndicatorView view = new AVLoadingIndicatorView(context);
        if (LOADING_MAP.get(type) == null){
            Indicator indicator = getIndicator(type);
            LOADING_MAP.put(type, indicator);
        }
        view.setIndicator(LOADING_MAP.get(type));
        return view;
    }

    private static Indicator getIndicator(String name){
        if (name == null || name.isEmpty()){
            return null;
        }
        StringBuilder drawableClassName = new StringBuilder();
        if (!name.contains(".")){
            String defaultPackageName = AVLoadingIndicatorView.class.getPackage().getName();
            drawableClassName.append(defaultPackageName)
                    .append(".indicators")
                    .append(".");
        }
        drawableClassName.append(name);
        try{
            Class<?> clazz = Class.forName(drawableClassName.toString());
            return (Indicator)clazz.newInstance();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }


}
